package com.cooba.activitys.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cooba.R;
import com.cooba.activitys.login.LoginActivity;
import com.cooba.utils.Md5Utils;
import com.cooba.utils.ServerURL;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import java.util.regex.Pattern;

/**
 * Created by Administrator on 2015/12/16.
 * 设置密码
 */
public class SetPasswordActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView mPhone_tv;
    private EditText mPwd_et;
    private EditText mPwd_comfirm_et;
    private Button mSubmit_btn;
    private String phoneNumber;
    private ImageButton mBackBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_password);

        PushAgent.getInstance(this).onAppStart();

        phoneNumber=getIntent().getStringExtra("phone");
        initView();
    }

    private void initView() {
        mPhone_tv = (TextView) findViewById(R.id.setPwd_phone_tv);
        mPwd_et = (EditText) findViewById(R.id.setPwd_pwd_et);
        mPwd_comfirm_et = (EditText) findViewById(R.id.setPwd_comfirm_pwd_et);
        mSubmit_btn = (Button) findViewById(R.id.setPwd_submit_btn);
        mBackBtn= (ImageButton) findViewById(R.id.setPwd_image_back);
        mPhone_tv.setText(phoneNumber);
        mSubmit_btn.setOnClickListener(this);
        mBackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        String password=mPwd_et.getText().toString().trim();
        String comfirm_pwd=mPwd_comfirm_et.getText().toString().trim();
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
        if (!password.matches(regex)){
           Toast.makeText(SetPasswordActivity.this,"密码格式不正确!",Toast.LENGTH_SHORT).show();
           return;
       }
        if (!password.equals(comfirm_pwd)){
            Toast.makeText(SetPasswordActivity.this,"两次输入的密码不一致!",Toast.LENGTH_SHORT).show();
            return;
        }
        String passwd=Md5Utils.encodeBy32BitMD5(password);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("login_id", phoneNumber);
        params.addBodyParameter("new_password", passwd);

        httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.ForgetPwd, params, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                Log.e("wei", "获取失败");
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    String msg = JSON.parseObject(responseInfo.result).getString("msg");
                    String status = JSON.parseObject(responseInfo.result).getString("status");
                    if (status.equals("1")) {
                        Toast.makeText(getApplication(), "重置密码成功!", Toast.LENGTH_SHORT).show();
                        //Intent intent = new Intent(getApplication(), LoginActivity.class);
                        //startActivity(intent);
                        ForgetPasswordActivity.instance.finish();
                        finish();
                    } else {
                        Toast.makeText(getApplication(), "重置密码失败!", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
